#include "seatdao.h"
#include <QSqlError>

SeatDao::SeatDao() {}

int SeatDao::insert(Seat &seat)
{
    // 插入数据
    QString insertQuery = "INSERT INTO seat (traynumber, seatnumber, seatdirection,note) "
                          "VALUES (:traynumber, :seatnumber, :seatdirection, :note)";
    qDebug() << insertQuery;
    query.prepare(insertQuery);
    query.bindValue(":traynumber", seat.getTrayNumber());
    query.bindValue(":seatnumber", seat.getSeatNumber());
    query.bindValue(":seatdirection", seat.getSeatDirection());
    query.bindValue(":note", seat.getNote());
    qDebug() << QString("%1,%2,%3,%4")
                    .arg(seat.getTrayNumber())
                    .arg(seat.getSeatNumber())
                    .arg(seat.getSeatDirection())
                    .arg(seat.getNote());

    if (!query.exec()) {
        qDebug() << QString("插入数据到 seat 表中失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    qDebug() << QString("插入数据到 seat 表中成功，最后一条记录的id是：%1 。")
                    .arg(query.lastInsertId().toInt());
    return query.lastInsertId().toInt();
}

int SeatDao::updateByPrimay(Seat &seat)
{
    QString updateQuery = "UPDATE seat Set traynumber = ?, seatnumber = ?, seatdirection = ?, note "
                          "= ? where id = ?";
    qDebug() << updateQuery;
    query.prepare(updateQuery);
    query.addBindValue(seat.getTrayNumber());
    query.addBindValue(seat.getSeatNumber());
    query.addBindValue(seat.getSeatDirection());
    query.addBindValue(seat.getNote());
    query.addBindValue(seat.getId());
    qDebug() << QString("%1,%2,%3,%4,%5")
                    .arg(seat.getTrayNumber())
                    .arg(seat.getSeatNumber())
                    .arg(seat.getSeatDirection())
                    .arg(seat.getNote())
                    .arg(seat.getId());

    if (!query.exec()) {
        qDebug() << QString("更新数据到 seat 表中失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    return 1;
}

int SeatDao::deleteByPrimay(Seat &seat)
{
    QString deleteQuery = "DELETE FROM seat where id = ?";
    query.prepare(deleteQuery);
    query.addBindValue(seat.getId());
    qDebug() << QString("%1").arg(seat.getId());
    if (!query.exec()) {
        qDebug() << QString("删除表格 seat 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return -1;
    }
    return 1;
}

int SeatDao::queryAll(QList<Seat *> &list)
{
    // 查询数据
    int count = 0;
    QString selectQuery = "SELECT * FROM seat";
    qDebug() << selectQuery;
    if (!query.exec(selectQuery)) {
        qDebug() << QString("查询表格 seat 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return count;
    }

    while (query.next()) {
        count++;
        int id = query.value("id").toInt();
        QString seatnumber = query.value("seatnumber").toString();
        QString traynumber = query.value("traynumber").toString();
        QString seatdirection = query.value("seatdirection").toString();
        QString note = query.value("note").toString();
        Seat *pSeat = new Seat(id, seatnumber, traynumber, seatdirection, note);
        list.append(pSeat);
    }

    return count;
}

int SeatDao::queryRecent(QList<Seat *> &list, int howMuch)
{
    // 查询数据
    int count = 0;
    QString selectQuery = QString("SELECT * FROM SEAT ORDER BY id DESC LIMIT '%1'").arg(howMuch);
    qDebug() << selectQuery;
    if (!query.exec(selectQuery)) {
        qDebug() << QString("查询表格 detection 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return count;
    }

    while (query.next()) {
        int id = query.value("id").toInt();
        QString seatnumber = query.value("seatnumber").toString();
        QString traynumber = query.value("traynumber").toString();
        QString seatdirection = query.value("seatdirection").toString();
        QString note = query.value("note").toString();
        Seat *pSeat = new Seat(id, seatnumber, traynumber, seatdirection, note);
        list.append(pSeat);
        count++;
    }

    return count;
}

int SeatDao::querySeatBySeatNumber(QList<Seat *> &list,QString seatNumber){
    // 查询数据
    int count = 0;
    QString selectQuery = QString("SELECT * FROM SEAT where seatnumber = '%1'").arg(seatNumber);
    qDebug() << selectQuery;
    if (!query.exec(selectQuery)) {
        qDebug() << QString("查询表格 seat 数据失败，失败原因是：%1 。")
                        .arg(query.lastError().text());
        return count;
    }

    while (query.next()) {
        count++;
        int id = query.value("id").toInt();
        QString seatnumber = query.value("seatnumber").toString();
        QString traynumber = query.value("traynumber").toString();
        QString seatdirection = query.value("seatdirection").toString();
        QString note = query.value("note").toString();
        Seat *pSeat = new Seat(id, seatnumber, traynumber, seatdirection, note);
        list.append(pSeat);
        count++;
    }

    return count;
}
